/**
 * 所有自定义控件命名控件 
 */
Helper.control = Helper.control ? Helper.control : {};
/**
 * 选项数据对象
 */
Helper.control.Data = function(value,label,index){
	this.value = value;
	this.index = index;
	this.label = label || value;
};
/**
 * 选项框类
 */
Helper.control.SelectContent = function(P){
	var params =  P || {};
	Helper.Components.call(this, P);
	var _aSelectValues = []; // 控件的值列表,该数据存放Helper.control.Data类型数据
	this.renderId = P.renderId;
	this.selCss = P.selCss || "list-Selected";
	this.delCallBack = P.delCallBack;//删除行成功后触发事件
	this.clearCallBack = P.clearCallBack;//清楚数据后触发事件
	this.insertCallBack = P.insertCallBack;//插入数据触发事件
	this.selectRowCallBack = P.selectRowCallBack;//选中行触发事件
	this.updateRowCallBack = P.updateRowCallBack;//修改触发事件
	
	var _sTopStr = "<ul class='betList'>";
	var _sBottomStr = "</ul>";
	var _sLiIdTop = this.renderId+"_SelectContent_id_";
	var THIS = this;
	if (typeof (Helper.control.SelectContent.__initialized) === "undefined") {
		// 刷新控件的值
		Helper.control.SelectContent.prototype.flush = function(){
			var _sContect = _sTopStr;
			for(var i=0;i<_aSelectValues.length;i+=1) {
				_sContect+="<li id='"+(_sLiIdTop+_aSelectValues[i].index.toString())+"'><span class='snum sssq'>"+_aSelectValues[i].label+"</span><a href='javascript:void(0)' class='sdel'>删除</a></li>";
			}
			_sContect += _sBottomStr;
			$("#"+THIS.renderId).html(_sContect);
			$("#"+THIS.renderId+" div ul li a").click(function(){
				var _sIdStr = $(this).parent().attr("id");
				var _nIndex = parseInt(_sIdStr.substr(_sLiIdTop.length),10);
				THIS.remove(_nIndex);
				return false;
			});
			$("#"+this.renderId+" div ul li").click(function(){
				$("#"+THIS.renderId+" div ul li").attr("class","");
				$(this).attr("class",THIS.selCss);
				if(THIS.selectRowCallBack) {
					var _sIdStr = $(this).attr("id");
					
					var _nIndex = parseInt(_sIdStr.substr(_sLiIdTop.length),10);
					THIS.selectRowCallBack(_nIndex);
				}
			});
		};
		// 添加数据
		Helper.control.SelectContent.prototype.add = function(value,label,index) {
			var _oData = new Helper.control.Data(value,label,index);
			_aSelectValues.push(_oData);
			var _update = "";
			var _ptype=Helper.lottery.lotteryData.playtype;
			if(THIS.updateRowCallBack && Helper.lottery.lotteryData && (_ptype===99 || _ptype===5)) {
				_update="<a href='javascript:void(0)' lang='update' class='sdel d_blue mr_10'>修改</a>";
			}
			var _sContent ="<li id='"+(_sLiIdTop+_oData.index.toString())+"'><span class='snum sssq'>"+(_oData.label || _oData.value)+"</span><a href='javascript:void(0)' class='sdel d_blue' lang='del'>删除</a>"+_update+"</li>";
			
			$("#"+this.renderId+" ul").append(_sContent);
			$("#"+(_sLiIdTop+_oData.index.toString())+" a[lang='del']").click(function(){
				var _sIdStr = $(this).parent().attr("id");
				var _nIndex = parseInt(_sIdStr.substr(_sLiIdTop.length),10);
				THIS.remove(_nIndex);
				return false;
			});
			$("#"+(_sLiIdTop+_oData.index.toString())+" a[lang='update']").click(function(){
				if(THIS.updateRowCallBack) {
					THIS.updateRowCallBack(_oData.index);
				}
				return false;
			});
			$("#"+(_sLiIdTop+_oData.index.toString())).click(function(){
				$("#"+THIS.renderId+" ul li").attr("class","");
				$(this).attr("class",THIS.selCss);
				
				if(THIS.selectRowCallBack) {
					THIS.selectRowCallBack(_oData.index);
				}
			});
			if(THIS.insertCallBack) {
				THIS.insertCallBack(_oData);
			}
		};
		// 删除数据
		Helper.control.SelectContent.prototype.remove = function(index){
			for (var i = 0; i < _aSelectValues.length; i += 1) {
				if(_aSelectValues[i].index === index) {
					_aSelectValues.splice(i,1);//移除数据
				}
			}
			$("#"+(_sLiIdTop+index.toString())).remove();
			if(THIS.delCallBack){
				THIS.delCallBack(index);
			}
		};
		// 清空数据
		Helper.control.SelectContent.prototype.clear = function() {
			_aSelectValues = [];
			THIS.flush();//刷新界面
			if(THIS.clearCallBack) {
				THIS.clearCallBack();
			}
		};
		// 获取控件的数据
		Helper.control.SelectContent.prototype.getData = function() {
			var _aTemp = [];
			for(var i=0;i<_aSelectValues.length;i+=1) {
				_aTemp[i] = _aSelectValues[i];
			}
			return _aTemp;
		};
		// 设置标识，已经初始化过原型方法
		Helper.control.SelectContent.__initialized = true;
	}	
};

/**
 * 选项数据对象
 */
Helper.control.SelectData = function(value,label,index,isSelect){
	this.value = value;
	this.label = label || value;
	this.isSelect = p.isSelect === undefined ? false : p.isSelect;
	
	this.index = index;
};

// Select下拉框控件
//[{value : 1,label : 1},{value : 1,label : 1,isSelect : true}]
Helper.control.Select = function(p){
	var params =  p || {};
	Helper.Components.call(this, p);
	this.renderId = p.renderId;
	this.index = new Helper.Index();
	this.data = null;//空间当前值
	this.onSelect = p.onSelect;//选中后出发的事件
	this.selClass = "fake-select hover";
	this.defaultClass = "fake-select";
	this.selectConectPrefix = "helper_select_";
	this.valueMap = new Helper.Map();
	if(p.values && p.values.length>0) {
		for(var i=0;i<p.values.length;i+=1) {
			this.add(p.values[i].value,p.values[i].label);
			if(p.values[i].isSelect) {
				this.data = p.values[i];
			}
		}
	}
};
Helper.control.Select.prototype.add = function(value,label) {
	this.valueMap.put(this.index.getIndex(),{value:value,label:label});
};
Helper.control.Select.prototype.flush = function(){
	 var THIS = this;
	 var _defaultContent = this.data && this.data.label;
	 var _sTop = "<span class='"+this.defaultClass+"'><ul class='selected'><li><s class='sel_dropdown'><s class='i'></s></s> <a href='javascript:void(0);'>"+(_defaultContent || "选择")+"</a></li></ul><ul class='toselect'>";
	 var _sBottom = "</ul></span>";
	 var _sContext = _sTop;
	 var _keys = this.valueMap.keys();
	 for(var i=0;i<_keys.length;i+=1) {
		 var _data = this.valueMap.get(_keys[i]);
		 _sContext+="<li><a name='"+this.selectConectPrefix+_keys[i]+"' href='javascript:void(0);'>"+_data.label+"</a></li>";
	 }
	 _sContext+=_sBottom;
	 $("#"+THIS.renderId).html(_sContext);
	 $("#"+THIS.renderId+">span>ul").mouseover(function(){
		 $("#"+THIS.renderId+">span").attr("class","fake-select hover");
	 });
	 $("#"+THIS.renderId+">span>ul").mouseout(function(){
		 $("#"+THIS.renderId+">span").attr("class","fake-select");
	 });
	 $("#"+THIS.renderId+">span>ul[class='toselect']>li>a").click(function(){
		 var _name = $(this).attr("name");
		 var _index = _name.substr(THIS.selectConectPrefix.length);
		 THIS.data = THIS.valueMap.get(Helper.doms.parseInt(_index, 10));
		 $("#"+THIS.renderId+">span>ul[class='selected']>li>a").html(THIS.data.label);
		 $("#"+THIS.renderId+">span").attr("class","fake-select");
		 if(typeof(THIS.onSelect) === "function") {
			 THIS.onSelect(THIS.data);
		 }
	 });
	 
};
Helper.control.Select.prototype.getValue = function(){
	if(this.data) {
		return this.data.value;
	} 
};
Helper.control.Select.prototype.getLabel = function(){
	if(this.data) {
		return this.data.label;
	} 
};
